package edu.unf.cen6940.dao;

import edu.unf.cen6940.domain.Category;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Category entities.
 * 
 */
@Repository("CategoryDAO")
@Transactional
public class CategoryDAOImpl extends AbstractJpaDao<Category> implements
		CategoryDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Category.class }));

	/**
	 * EntityManager injected by Spring for persistence unit rms
	 *
	 */
	@PersistenceContext(unitName = "rms")
	private EntityManager entityManager;

	/**
	 * Instantiates a new CategoryDAOImpl
	 *
	 */
	public CategoryDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAllCategorys
	 *
	 */
	@Transactional
	public Set<Category> findAllCategorys() throws DataAccessException {

		return findAllCategorys(-1, -1);
	}

	/**
	 * JPQL Query - findAllCategorys
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Category> findAllCategorys(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCategorys", startResult, maxRows);
		return new LinkedHashSet<Category>(query.getResultList());
	}
	
	@Transactional
	public Set<Category> findRandomCategorys() throws DataAccessException {

		return findRandomCategorys(-1, -1);
	}

	/**
	 * JPQL Query - findAllCategorys
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Category> findRandomCategorys(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRandomCategorys", startResult, maxRows);
		return new LinkedHashSet<Category>(query.getResultList());
	}

	/**
	 * JPQL Query - findCategoryByPrimaryKey
	 *
	 */
	@Transactional
	public Category findCategoryByPrimaryKey(Integer categoryId) throws DataAccessException {

		return findCategoryByPrimaryKey(categoryId, -1, -1);
	}

	/**
	 * JPQL Query - findCategoryByPrimaryKey
	 *
	 */

	@Transactional
	public Category findCategoryByPrimaryKey(Integer categoryId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCategoryByPrimaryKey", startResult, maxRows, categoryId);
			return (edu.unf.cen6940.domain.Category) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findCategoryByCategoryId
	 *
	 */
	@Transactional
	public Category findCategoryByCategoryId(Integer categoryId) throws DataAccessException {

		return findCategoryByCategoryId(categoryId, -1, -1);
	}

	/**
	 * JPQL Query - findCategoryByCategoryId
	 *
	 */

	@Transactional
	public Category findCategoryByCategoryId(Integer categoryId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCategoryByCategoryId", startResult, maxRows, categoryId);
			return (edu.unf.cen6940.domain.Category) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findCategoryByCategoryDescriptionContaining
	 *
	 */
	@Transactional
	public Set<Category> findCategoryByCategoryDescriptionContaining(String categoryDescription) throws DataAccessException {

		return findCategoryByCategoryDescriptionContaining(categoryDescription, -1, -1);
	}

	/**
	 * JPQL Query - findCategoryByCategoryDescriptionContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Category> findCategoryByCategoryDescriptionContaining(String categoryDescription, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCategoryByCategoryDescriptionContaining", startResult, maxRows, categoryDescription);
		return new LinkedHashSet<Category>(query.getResultList());
	}

	/**
	 * JPQL Query - findCategoryByCategoryDescription
	 *
	 */
	@Transactional
	public Set<Category> findCategoryByCategoryDescription(String categoryDescription) throws DataAccessException {

		return findCategoryByCategoryDescription(categoryDescription, -1, -1);
	}

	/**
	 * JPQL Query - findCategoryByCategoryDescription
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Category> findCategoryByCategoryDescription(String categoryDescription, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCategoryByCategoryDescription", startResult, maxRows, categoryDescription);
		return new LinkedHashSet<Category>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Category entity) {
		return true;
	}

	@Override
	public Set<Category> searchByTerm(String term) throws DataAccessException {
		Query query = createNamedQuery("findCategoryByCategoryDescriptionContaining", -1, -1, "%"+term+"%");
		return new LinkedHashSet<Category>(query.getResultList());
	}
}
